package com.fht.service.inventory.impl;

import com.fht.constant.ManagermentSystemConstant;
import com.fht.entity.FoodRecord;
import com.fht.entity.FoodTasteCategory;
import com.fht.entity.PurchaseOrders;
import com.fht.from.FoodCategoryFrom;
import com.fht.from.inventoryFrom;
import com.fht.mapper.inventory.PurchaseOrdersMapper;
import com.fht.service.inventory.PurchaseOrdersService;
import com.fht.vo.LayUITableVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class PurchaseOrdersServiceImpl implements PurchaseOrdersService {


    @Resource
    public PurchaseOrdersMapper purchaseOrdersMapper;


    @Override
    public LayUITableVO<List<PurchaseOrders>> findPage(Map<String, Object> map) {


        inventoryFrom from = (inventoryFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(FoodRecord.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("merchantsId",from.getMerchantsId());
        if (! "".equals(from.getOrderId()) && null!=from.getOrderId()){
            c.andLike("id","%"+from.getOrderId()+"%");
        }

        List<PurchaseOrders> list = purchaseOrdersMapper.selectByExample(example);
        for (PurchaseOrders purchaseOrders : list) {
           this.formatConversion(purchaseOrders);
        }

        PageInfo<PurchaseOrders> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<PurchaseOrders>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public PurchaseOrders selectById(String id) {

        PurchaseOrders purchaseOrders = purchaseOrdersMapper.selectByPrimaryKey(id);
        this.formatConversion(purchaseOrders);

        return purchaseOrders;
    }

    @Override
    @Transactional
    public void update(PurchaseOrders purchaseOrders) {


        PurchaseOrders old = purchaseOrdersMapper.selectByPrimaryKey(purchaseOrders.getId());
        if (null == old || "".equals(old)){

            contraryFormatConversion(purchaseOrders);

            purchaseOrdersMapper.insertSelective(purchaseOrders);

        }else {

            contraryFormatConversion(purchaseOrders);
            purchaseOrdersMapper.updateByPrimaryKeySelective(purchaseOrders);
        }



    }

    @Override
    @Transactional
    public void deleteById(String id) {
        purchaseOrdersMapper.deleteByPrimaryKey(id);
    }


    public void contraryFormatConversion(PurchaseOrders purchaseOrders){
        if ("未审核".equals(purchaseOrders.getStatusString())){
            purchaseOrders.setStatus(1);
        }else if ("审核成功".equals(purchaseOrders.getStatusString())){
            purchaseOrders.setStatus(2);
        }else if ("审核失败".equals(purchaseOrders.getStatusString())){
            purchaseOrders.setStatus(3);
        }

        if ("".equals(purchaseOrders.getPreparedDate())){
            purchaseOrders.setPreparedDate(null);
        }
        if ("".equals(purchaseOrders.getVerifierDate())){
            purchaseOrders.setVerifierDate(null);
        }
    }

    //对象格式转换（时间，状态）
    public void formatConversion(PurchaseOrders purchaseOrders){
        String preparedDate = purchaseOrders.getPreparedDate();
        if (!"".equals(preparedDate) && null != preparedDate) {
            preparedDate = preparedDate.substring(0, preparedDate.lastIndexOf("."));
            purchaseOrders.setPreparedDate(preparedDate);
        }

        String verifierDate = purchaseOrders.getVerifierDate();
        if (!"".equals(verifierDate) && null != verifierDate) {
            verifierDate = verifierDate.substring(0, verifierDate.lastIndexOf("."));
            purchaseOrders.setVerifierDate(verifierDate);
        }
        Integer status = purchaseOrders.getStatus();
        if (1==status){
            purchaseOrders.setStatusString("未审核");
        }else if (2 == status){
            purchaseOrders.setStatusString("审核成功");
        }else if (3 == status){
            purchaseOrders.setStatusString("审核失败");
        }else {
            purchaseOrders.setStatusString("未知状态");
        }

        purchaseOrders.setTotalString(String.valueOf(purchaseOrders.getTotal()/ ManagermentSystemConstant.PRICE_TRANSITION));

    }
}
